﻿using System;
using System.Threading;
using RaisingStudio.SmallProgram.Library;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using Windows.UI.Xaml.Media;

namespace RaisingStudio.SmallProgram.Library.Internal
{
    public static class MessageBoxHelper
    {
        public static void ShowMessageBox(Panel parent, string message, string title, ManualResetEvent resetEvent)
        {
            var CloseButton =
                                new Button
                                {
                                    Name = "CloseButton",
                                    Content = "Close",
                                    Margin = new Thickness { Left = 4.0, Top = 4.0, Right = 4.0, Bottom = 4.0 },
                                    Padding = new Thickness { Left = 4.0, Top = 4.0, Right = 4.0, Bottom = 4.0 },
                                    Width = 70.0,
                                    HorizontalAlignment = HorizontalAlignment.Right
                                };
            Grid grid = new Grid
            {
                Background = new SolidColorBrush(new Color { A = 0x80, R = 0, G = 0, B = 0 }),
                Children = 
                {
                    new Border
                    {
                        Background = new SolidColorBrush(Colors.White),
                                    Margin = new Thickness { Left = 4.0, Top = 4.0, Right = 4.0, Bottom = 4.0 },
                                    Padding = new Thickness { Left = 4.0, Top = 4.0, Right = 4.0, Bottom = 4.0 },
                        BorderThickness = new Thickness{ Left = 1.0, Top = 1.0, Right = 1.0, Bottom = 4.0 },
                        BorderBrush = new SolidColorBrush(Colors.Black),
                        Width = 320.0,
                        VerticalAlignment = VerticalAlignment.Center,
                        Child = new StackPanel
                        {
                            Orientation = Orientation.Vertical,
                            Children =        
                            {
                                new Border
                                {
                                    Background = new SolidColorBrush(Colors.Gray),
                                    Margin = new Thickness { Left = 0, Top = 0, Right = 0, Bottom = 0 },
                                    Padding = new Thickness { Left = 2.0, Top = 2.0, Right = 2.0, Bottom = 2.0 },
                                    Child = new TextBlock
                                    {
                                        Text = title,
                                        FontSize = 14.0,
                                        FontWeight = FontWeights.Bold,
                                        HorizontalAlignment = HorizontalAlignment.Stretch,
                                        Foreground = new SolidColorBrush(Colors.White)
                                    }
                                },                        
                                new TextBlock
                                {
                                    Margin = new Thickness { Left = 4.0, Top = 4.0, Right = 4.0, Bottom = 4.0 },
                                    HorizontalAlignment = HorizontalAlignment.Stretch,
                                    TextWrapping = TextWrapping.Wrap,
                                    Text = message            
                                },
                                CloseButton
                            }            
                        }
                    }
                }
            };
            CloseButton.Click += delegate
                {
                    parent.Children.Remove(grid);
                    resetEvent.Set();
                    GraphicsWindow._keyboardControl.Focus();
                };
            parent.Children.Add(grid);
        }
    }
}